$(function () {
  // const lately = new Swiper('#lately', {
  //   mousewheel: true,
  //   freeMode: true,
  //   pagination: {
  //     el: '.swiper-pagination',
  //     type: 'progressbar',
  //   },
  // });
  let wallpaper = {
    height: 0,
    isHeight: false
  };
  window.onscroll = e => {
    setWallpaperHeight(e.target.scrollingElement.scrollTop);
  }
  /**
   * 
   * @param {Int} scrollTop 距离顶部距离
   */
  function setWallpaperHeight(scrollTop) {
    if (!wallpaper.isHeight) {
      wallpaper.isHeight = $('#app-background').height();
    }
    wallpaper.height = wallpaper.isHeight - scrollTop;
    if (wallpaper.height <= 90) {
      wallpaper.height = 90;
    }
    $('#app-background').height(wallpaper.height)
  }
  // console.log(chrome.history)
  // console.log(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)
  chrome.history.search({
    text: '',
    startTime: new Date('2020-9-28').getTime(),
    endTime: new Date().getTime(),
    maxResults: 100
  }, (e) => {
    let html = '';
    // let elm = '';
    let res = mergeData(e, 'title')
    res.forEach((item, i) => {
      // if (i < 12) {
      // html += getHtml(item, 'history' + i);
      $('#lately').append(getHtml(item, 'history' + i))
      // if (i % 12 == 11) {
      //   lately.appendSlide('<div class="swiper-slide">' + html + '</div>');
      //   html = ''
      // }
    });
    // $('#lately').append(html);
    // lately.appendSlide(['<div class="swiper-slide">' + html + '</div>', '<div class="swiper-slide">' + html + '</div>']);
  });
  // 当前访问
  chrome.tabs.query({
    title: ''
  }, res => {
    let html = ''
    res.forEach((item, i) => {
      if (item.url != 'chrome://newtab/') {
        getImage(item.favIconUrl).then(res => {
          $('#img-current' + i).attr('src', res);
        }).catch(() => {
          $('#img-current' + i).attr('src', '/icon/default-icon.png');
        })
        // html += '<li class="current-tab_item">\
        //         <a href="' + item.url + '">\
        //           <div class="icon"><img id="img-current' + i + '"/></div>\
        //           <p class="title">' + item.title + '</p>\
        //         </a>\
        //         <button class="btn"></button>\
        //       </li>';
        html += '<li class="current-tab_item">\
                <a href="' + item.url + '">\
                  <div class="icon"><img id="img-current' + i + '"/></div>\
                  <p class="title">' + item.title + '</p>\
                </a>\
              </li>';
      }
    })
    $('#current').append(html);
    setTimeout(() => {
      setWallpaperHeight($(window).scrollTop());
    }, 300)

  });


  //获取收藏
  chrome.bookmarks.getTree(function (res) {
    console.log(res);
  });
  var searchHistory = localStorage.getItem('search-history');
  if (searchHistory) {
    searchHistory = searchHistory.split(',');
  } else {
    searchHistory = []
  }
  
  $('#search-input').on('input', function (e) {
    // console.log(e.target.value)
  }).on('keyup', function (e) {
    if (e.keyCode === 13) {
      var val = e.target.value;
      if ($.trim(val) != '') {
        if (searchHistory.indexOf(val) == -1) {
          searchHistory.push(val);
          localStorage.setItem('search-history', searchHistory.join(','));
        }
      }
      $(this).val('');
      // window.location.href = 'https://www.baidu.com/s?wd=' + val;
    }
  })
});